/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (theLicense");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-20
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "CardViewComponentInstance.h"
#include "Props.h"
#include "CardViewStackNode.h"

namespace rnoh {
    CardViewComponentInstance::CardViewComponentInstance(Context context) : CppComponentInstance(std::move(context)) {}

    void CardViewComponentInstance::onChildInserted(ComponentInstance::Shared const &childComponentInstance,
                                                    std::size_t index)
    {
        m_stackNode.insertChild(childComponentInstance->getLocalRootArkUINode(), index);
    }

    void CardViewComponentInstance::onChildRemoved(ComponentInstance::Shared const &childComponentInstance)
    {
        m_stackNode.removeChild(childComponentInstance->getLocalRootArkUINode());
    };

    CardViewStackNode &CardViewComponentInstance::getLocalRootArkUINode() { return m_stackNode; }

        void CardViewComponentInstance::onPropsChanged(SharedConcreteProps const &props)
    {
        CppComponentInstance::onPropsChanged(props);
        if (auto p = std::dynamic_pointer_cast<const facebook::react::RNCardViewProps>(props)) {
            this->backgroundColor = p->backgroundColor;
            this->cornerRadius = p->cornerRadius;
            this->cardElevation = p->cardElevation;
            this->cardMaxElevation = p->cardMaxElevation;
            this->getLocalRootArkUINode().setBackgroundColor(p->backgroundColor);
            this->getLocalRootArkUINode().setCornerRadius(p->cornerRadius);
            this->getLocalRootArkUINode().setCardElevation(p->cardElevation);
            this->getLocalRootArkUINode().setCardMaxElevation(p->cardMaxElevation);
        }
    }
    void CardViewComponentInstance::getNapiProps(facebook::react::Props::Shared props)
        {
            auto p = std::dynamic_pointer_cast<const facebook::react::RNCardViewProps>(props);
            this->backgroundColor = p->backgroundColor;
            this->cornerRadius = p->cornerRadius;
            this->cardElevation = p->cardElevation;
            this->cardMaxElevation = p->cardMaxElevation;
        }
}